<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"anynone.gitee.io","root":"/","images":"/images","scheme":"Pisces","darkmode":true,"version":"8.7.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="Linux，全称GNU&#x2F;Linux，是一种免费使用和自由传播的类UNIX操作系统，其内核由林纳斯·本纳第克特·托瓦兹于1991年10月5日首次发布，它主要受到Minix和Unix思想的启发，是一个基于POSIX的多用户、多任务、支持多线程和多CPU的操作系统。它能运行主要的Unix工具软件、应用程序和网络协议。它支持32位和64位硬件。Linux继承了Unix以网络为核心的设计思想，是一个性能稳">
<meta property="og:type" content="article">
<meta property="og:title" content="聊聊linux源码编译">
<meta property="og:url" content="https://anynone.gitee.io/2022/04/07/linux-build-sometime/index.html">
<meta property="og:site_name" content="anynone">
<meta property="og:description" content="Linux，全称GNU&#x2F;Linux，是一种免费使用和自由传播的类UNIX操作系统，其内核由林纳斯·本纳第克特·托瓦兹于1991年10月5日首次发布，它主要受到Minix和Unix思想的启发，是一个基于POSIX的多用户、多任务、支持多线程和多CPU的操作系统。它能运行主要的Unix工具软件、应用程序和网络协议。它支持32位和64位硬件。Linux继承了Unix以网络为核心的设计思想，是一个性能稳">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://anynone.gitee.io/images/linux-compile/2022-04-15_00-07.png">
<meta property="og:image" content="https://anynone.gitee.io/images/linux-compile/2022-04-15_00-10.png">
<meta property="og:image" content="https://anynone.gitee.io/images/linux-compile/2022-04-15_00-17.png">
<meta property="og:image" content="https://anynone.gitee.io/images/linux-compile/2022-04-15_00-22.png">
<meta property="og:image" content="https://anynone.gitee.io/images/linux-compile/2022-04-15_00-33.png">
<meta property="article:published_time" content="2022-04-07T15:01:20.000Z">
<meta property="article:modified_time" content="2022-09-09T00:09:56.415Z">
<meta property="article:author" content="山丘之王">
<meta property="article:tag" content="linux,build">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://anynone.gitee.io/images/linux-compile/2022-04-15_00-07.png">


<link rel="canonical" href="https://anynone.gitee.io/2022/04/07/linux-build-sometime/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://anynone.gitee.io/2022/04/07/linux-build-sometime/","path":"2022/04/07/linux-build-sometime/","title":"聊聊linux源码编译"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>聊聊linux源码编译 | anynone</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">anynone</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <img src="/images/head.jpg">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-6"><a class="nav-link" href="#nginx-http-nginx-org-download-nginx-1-21-6-tar-gz"><span class="nav-number">1.</span> <span class="nav-text">[nginx]: http:&#x2F;&#x2F;nginx.org&#x2F;download&#x2F;nginx-1.21.6.tar.gz</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#%E7%99%BE%E5%BA%A6%E7%99%BE%E7%A7%91-https-baike-baidu-com-item-Linux-27050-fr-aladdin"><span class="nav-number">2.</span> <span class="nav-text">[百度百科]:  https:&#x2F;&#x2F;baike.baidu.com&#x2F;item&#x2F;Linux&#x2F;27050?fr&#x3D;aladdin</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">山丘之王</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">22</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/anynone" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;anynone" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://anynone.gitee.io/2022/04/07/linux-build-sometime/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="山丘之王">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="anynone">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          聊聊linux源码编译
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-04-07 23:01:20" itemprop="dateCreated datePublished" datetime="2022-04-07T23:01:20+08:00">2022-04-07</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-09-09 08:09:56" itemprop="dateModified" datetime="2022-09-09T08:09:56+08:00">2022-09-09</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <blockquote>
<p>Linux，全称GNU/Linux，是一种免费使用和自由传播的类UNIX操作系统，其内核由林纳斯·本纳第克特·托瓦兹于1991年10月5日首次发布，它主要受到Minix和Unix思想的启发，是一个基于POSIX的多用户、多任务、支持多线程和多CPU的操作系统。它能运行主要的Unix工具软件、应用程序和网络协议。它支持32位和64位硬件。Linux继承了Unix以网络为核心的设计思想，是一个性能稳定的多用户网络操作系统。Linux有上百种不同的发行版，如基于社区开发的debian、archlinux，和基于商业开发的Red Hat Enterprise Linux、SUSE、Oracle Linux等。</p>
</blockquote>
<p>linux通过编译构建一个软件对大部分程序猿可能比较陌生，最近构建了一个比较老版本的Docker镜像。出于某些需求，只能使用比较老的软件和系统版本。随之而来的就是各种奇怪的报错，甚至源码语法错误。当然，有时候也很难说清楚错误的具体原因。对于用过一段时间linux的用户来说，编译安装软件和windows双击运行，然后一路下一步没有太大区别。事实确实也是如此，linux编译安装一款软件一般只需要三个命令</p>
<ul>
<li>configure 配置构建参数</li>
<li>make  编译</li>
<li>make install 安装</li>
</ul>
<p>比较容易出现问题的是configure和make。configure阶段按照参数配置编译选项，比如安装装到哪个目录，启用什么功能和依赖是否缺失等，一般都通过这个命令配置。make命令则是实实在在的编译和连接过程。make install可以理解成就是文件复制到安装目录。幸运的是，所有软件configure时配置安装目录的参数都是<code>--prefix</code>，比如下面nginx的安装命令表示将nginx安装到/usr/local/nginx目录</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./configure --prefix=/usr/<span class="built_in">local</span>/nginx</span><br></pre></td></tr></table></figure>

<p>这是编译最常用的选项了。其他选项可以使用-h或–help参数查看，比如下面nginx的部分帮助信息</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./configure --<span class="built_in">help</span></span><br></pre></td></tr></table></figure>

<img src="/images/linux-compile/2022-04-15_00-07.png" alt="2022-04-15_00-07" style="zoom:80%;margin-left:0px;" />

<p>有些源码并没有configure可执行文件，比如下面是boost的源码目录</p>
<img src="/images/linux-compile/2022-04-15_00-10.png" alt="2022-04-15_00-10" style="zoom:100%;margin-left:0px;" />

<p>能看到源码中没有configure文件。这种就需要看源码目录的INSTALL或者README文件了。当然，对于编译安装很熟练的linux老手看到bootstrap.sh基本也不用看INSTALL。直接查看bootstrap.sh提供的帮助信息即可。(<u><strong>一般来说，编译前先用ls命令看文件夹里有没有绿色的可执行文件，如果类似bootstrap,config,configure等，基本就是编译配置命令，如果有Makefile文件，则可以直接make安装</strong></u>)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./bootstrap.sh --help</span><br></pre></td></tr></table></figure>


<div style="display:flex;">
<img src="/images/linux-compile/2022-04-15_00-17.png" alt="2022-04-15_00-17" style="zoom:75%;margin-left:0px;" /><img src="/images/linux-compile/2022-04-15_00-22.png" alt="2022-04-15_00-22" style="zoom:75%;" />
</div>

<p>基于此便可以构建boost，构建命令如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">./bootstrap.sh --with-libraries=program_options <span class="comment">## 仅编译program_options库</span></span><br><span class="line">./bjam install <span class="comment">## 安装</span></span><br></pre></td></tr></table></figure>

<p>其实，configure操作并不是必须的，应为configure命令仅仅是配置编译选项，检查依赖是否完整等预处理操作，最终生成Makefile。Makefile后面再说，只需要知道是make命令需要的一个文件，有Makefile就可以使用make进行编译。比如hiredis的源码包就不需要configure。</p>
<img src="/images/linux-compile/2022-04-15_00-33.png" alt="2022-04-15_00-33" style="zoom:100%;margin-left:0px;" />

<p>这种安装包使用下面命令编译即可.如果想要改变编译参数则需要修改Makefile</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">make -j 4 &amp;&amp; make install</span><br></pre></td></tr></table></figure>

<p>除了make外，还有一个很常用的编译工具是cmake。cmake是一款跨平台项目管理工具，cmake属于make的下层工具，用来生成make所需的Makefile。对于小型项目，直接上手编写Makefile是没有什么问题的。但是对于大型项目，上手直接编写Makefile不是一件简单的事情，应该考虑自动生成Makefile。大多数linux系统可以直接通过包管理工具安装cmake，使用方式也很简单。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 新建build文件夹，文件夹名称无所谓，习惯上用build。特殊编译方式参考README</span></span><br><span class="line">mkdir build &amp;&amp; pushd build</span><br><span class="line"><span class="comment">// 编译</span></span><br><span class="line">cmake ../</span><br><span class="line"><span class="comment">// 安装</span></span><br><span class="line">make install</span><br></pre></td></tr></table></figure>



<p><code>相关源码和文献</code></p>
<h6 id="nginx-http-nginx-org-download-nginx-1-21-6-tar-gz"><a href="#nginx-http-nginx-org-download-nginx-1-21-6-tar-gz" class="headerlink" title="[nginx]: http://nginx.org/download/nginx-1.21.6.tar.gz"></a>[nginx]: <a target="_blank" rel="noopener" href="http://nginx.org/download/nginx-1.21.6.tar.gz">http://nginx.org/download/nginx-1.21.6.tar.gz</a></h6><h6 id="百度百科-https-baike-baidu-com-item-Linux-27050-fr-aladdin"><a href="#百度百科-https-baike-baidu-com-item-Linux-27050-fr-aladdin" class="headerlink" title="[百度百科]:  https://baike.baidu.com/item/Linux/27050?fr=aladdin"></a>[百度百科]:  <a target="_blank" rel="noopener" href="https://baike.baidu.com/item/Linux/27050?fr=aladdin">https://baike.baidu.com/item/Linux/27050?fr=aladdin</a></h6>
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/linux-build/" rel="tag"># linux,build</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/04/07/qt-connect/" rel="prev" title="qt信号和槽函数">
                  <i class="fa fa-chevron-left"></i> qt信号和槽函数
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/04/20/20220420-dockerfile-build/" rel="next" title="Dockerfile编写和镜像构建">
                  Dockerfile编写和镜像构建 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2021 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">山丘之王</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
